<?php

class withdraw_ewallet_form extends CFormModel {

    public $serial_pin;
    public $kode_unik;
    public $jumlah_ditransfer;
    public $transfer_to;

    /**
     * Declares the validation rules.
     */
    public function rules() {
        return array(
            //array('serial_pin, jumlah_ditransfer', 'required', 'message' => '{attribute} tidak boleh kososng'),
            array('jumlah_ditransfer', 'validasi_jumlah'),
            array('serial_pin', 'validasi_pin'),
            // verifyCode needs to be entered correctly
            //array('kode_unik', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(), 'message' => ' Kode unik salah'),
        );
    }

    public function validasi_jumlah($attribute, $params) {

        
        
        $saldo_ewallet = Yii::app()->db->createCommand()
                ->select('ewallet_cash_acc,ewallet_cash_paid')
                ->from('sys_ewallet')
                ->where('ewallet_network_id=:id', array(':id' => Yii::app()->user->getState('network_id')))
                ->queryRow();

        $saldo_ewallet = $saldo_ewallet['ewallet_cash_acc'] - $saldo_ewallet['ewallet_cash_paid'];
        $nominal_withdraw = $_POST['withdraw_ewallet_form']['jumlah_ditransfer'];

        if( $nominal_withdraw < 1 ){
            $this->addError('jumlah_ditransfer', 'Isikan jumlah yang mau ditransfer.');
        } elseif ($saldo_ewallet < $nominal_withdraw) {
            //saldo ewallet tidak cukup
//            $messages = Yii::t('yii', "Saldo Ewallet Anda tidak mencukupi");
//            $this->addError($attribute, $messages);
            $this->addError('jumlah_ditransfer', 'Saldo Ewallet Anda tidak mencukupi.');
        } elseif ($nominal_withdraw < 200000) {
            //saldo ewallet tidak cukup
//            $messages = Yii::t('yii', "Saldo Ewallet Anda tidak mencukupi");
//            $this->addError($attribute, $messages);
            $this->addError('jumlah_ditransfer', 'Minimal Transfer E-wallet sebesar Rp 200.000.');
        }
    }

    public function validasi_pin($attribute, $params) {

        $pin = $_POST['withdraw_ewallet_form']['serial_pin'];
        if (!function_lib::pin_validation(Yii::app()->user->getId(), $pin)) {
//            $messages = Yii::t('yii', "Pin Salah");
//            $this->addError($attribute, $messages);
            $this->addError('serial_pin', 'Pin Anda Salah.');
        }
    }

}

?>
